<?php
/***********************************************************
	Filename: app/www/control/post.php
	Note	: 信息发布
	Version : 3.0
	Author  : qinggan
	Update  : 2010-05-04
***********************************************************/
class post_c extends Control
{
	function __construct()
	{
		parent::Control();
		$this->load_model("post");
		$this->load_model("module");
		$this->load_model("cate");
	}

	function index_c()
	{
		$this->__construct();
	}

	function _load_moduel($module_id)
	{
		$m_rs = $this->module_m->get_one($module_id);
		if(!$m_rs || !$m_rs["status"] || $m_rs["ctrl_init"] != "list")
		{
			error("没有找到有效模块信息！",$this->url());
		}
		if($_SESSION["user_id"])
		{
			if(!$m_rs["if_post"])
			{
				error("您不支持该模块操作！",$this->url());
			}
		}
		else
		{
			if(!$m_rs["if_guest"])
			{
				error("您不支持该模块操作！",$this->url());
			}
		}
		$this->tpl->assign("m_rs",$m_rs);
		return $m_rs;
	}

	function list_f()
	{
		$page_url = $this->url("post,list");
		$module_id = $this->trans_lib->int("module_id");
		if(!$module_id)
		{
			$ms = $this->trans_lib->safe("ms");
			if($ms)
			{
				$module_id = $this->module_m->get_mid_from_code($ms);
			}
			if(!$module_id)
			{
				error("没有指定模块ID",$this->url());
			}
		}
		$this->tpl->assign("module_id",$module_id);
		$page_url .= "module_id=".$module_id."&";
		$m_rs = $this->_load_moduel($module_id);
		//取得模块列表
		$ifcate = $m_rs["if_cate"] ? true : false;
		$ifbiz = $m_rs["if_biz"] ? true : false;
		$ifthumb = $m_rs["if_thumb"] ? true : false;
		$this->tpl->assign("ifcate",$ifcate);
		$pageid = $this->trans_lib->int(SYS_PAGEID);
		$this->post_m->set_condition("m.author='".$_SESSION["user_name"]."'");
		$this->post_m->set_condition("m.author_type='user'");
		$this->post_m->set_condition("m.langid='".$_SESSION["sys_lang_id"]."'");//区分语言
		$this->post_m->set_condition("m.module_id='".$module_id."'");
		if($ifcate)
		{
			$cate_id = $this->trans_lib->int("cate_id");
			if($cate_id>0)
			{
				$page_url .= "cate_id=".$cate_id."&";
				$this->post_m->set_condition("m.cate_id='".$cate_id."'");
			}
		}
		$keywords = $this->trans_lib->safe("keywords");
		if($keywords)
		{
			$this->post_m->set_condition("m.title LIKE '%".$keywords."%'");
			$page_url .= "keywords=".rawurlencode($keywords)."&";
		}
		$rslist = $this->post_m->get_list($pageid,$ifcate,$ifbiz,$ifthumb);
		$this->tpl->assign("rslist",$rslist);
		$total_count = $this->post_m->get_count();//取得总数
		$pagelist = $this->page_lib->page($page_url,$total_count,false,false);
		$this->tpl->assign("del_refresh_url",$page_url.SYS_PAGEID."=".$pageid);
		$this->tpl->assign("pagelist",$pagelist);
		//如果有分类
		if($ifcate)
		{
			$condition = $_SESSION["user_id"] ? "ifuser='1'" : "ifguest='1'";
			$this->cate_m->get_catelist($module_id,$condition);
			$cate_html = $this->cate_m->html_select("cate_id",$cate_id,"全部分类");
			$this->tpl->assign("cate_html",$cate_html);
		}
		$tplfile = "post_list_".$m_rs["identifier"];
		$chk_tplfile = ROOT.$this->tpl->tpldir."/".$tplfile.".".$this->tpl->ext;
		if(file_exists($chk_tplfile))
		{
			$this->tpl->display($tplfile.".".$this->tpl->ext);
		}
		else
		{
			$this->tpl->display("post_list.".$this->tpl->ext);
		}
	}

	function set_f()
	{
		$id = $this->trans_lib->int("id");
		if($id)
		{
			$rs = $this->post_m->get_one($id);
			$this->tpl->assign("rs",$rs);
			$this->tpl->assign("id",$id);
			$module_id = $rs["module_id"];
			$cate_id = $rs["cate_id"];
		}
		else
		{
			$module_id = $this->trans_lib->int("module_id");
			$cate_id = $this->trans_lib->int("cate_id");
		}
		if(!$module_id)
		{
			$ms = $this->trans_lib->safe("ms");
			if($ms)
			{
				$module_id = $this->module_m->get_mid_from_code($ms);
			}
			if(!$module_id)
			{
				error("没有指定模块ID",$this->url());
			}
		}
		$this->tpl->assign("module_id",$module_id);
		$m_rs = $this->_load_moduel($module_id);
		//读取内容
		$ifcate = $m_rs["if_cate"] ? true : false;
		$this->tpl->assign("ifcate",$ifcate);
		if($ifcate)
		{
			$condition = $_SESSION["user_id"] ? "ifuser='1'" : "ifguest='1'";
			$this->cate_m->get_catelist($module_id,$condition);
			//$cate_id = $this->trans_lib->int("cate_id");
			$cate_html = $this->cate_m->html_select("cate_id",$cate_id,"请选择分类");
			$this->tpl->assign("cate_html",$cate_html);
		}
		$ext_list = $this->module_m->fields_index($module_id);
		//$ext_list = $this->_load_ext_fields($module_id);//获取扩展信息
		if($ext_list && is_array($ext_list) && count($ext_list)>0)
		{
			$optlist = array();
			$this->load_lib("phpok_input");
			$extlist_must = $extlist_need = array();
			foreach($ext_list AS $key=>$value)
			{
				if(!$value["if_post"] && !$value["if_guest"])
				{
					continue;
				}
				$_field_name = $value["identifier"];
				$value["default_val"] = $rs[$_field_name] ? $rs[$_field_name] : $value["default_val"];
				$extlist = $this->phpok_input_lib->get_html($value);
				if($value["if_must"])
				{
					$extlist_must[] = $extlist;
				}
				else
				{
					$extlist_need[] = $extlist;
				}
				if($value["input"] == "opt")
				{
					$optlist[] = $value;
				}
				$ext_list[$key] = $value;
			}
			$this->tpl->assign("extlist_must",$extlist_must);
			$this->tpl->assign("extlist_need",$extlist_need);
			$this->tpl->assign("optlist",$optlist);
			$this->tpl->assign("extlist",$ext_list);
		}
		$tplfile = "post_".$m_rs["identifier"];
		$chk_tplfile = ROOT.$this->tpl->tpldir."/".$tplfile.".".$this->tpl->ext;
		if(file_exists($chk_tplfile))
		{
			$this->tpl->display($tplfile.".".$this->tpl->ext);
		}
		else
		{
			$this->tpl->display("post_set.".$this->tpl->ext);
		}
	}

	function setok_f()
	{
		$id = $this->trans_lib->int("id");
		if(!$id)
		{
			$module_id = $this->trans_lib->int("module_id");
			if(!$module_id)
			{
				error("对不起，您的操作错误，没有指定应用模块",$this->url());
			}
		}
		else
		{
			$rs = $this->post_m->get_one($id);
			$module_id = $rs["module_id"];
		}
		$m_rs = $this->_load_moduel($module_id);
		//获取核心数据
		$array_sys = array();
		if(!$id)
		{
			$array_sys["module_id"] = $module_id;
		}
		$array_sys["cate_id"] = $this->trans_lib->int("cate_id");
		$cateid = $array_sys["cate_id"];
		$array_sys["title"] = $this->trans_lib->safe("subject");
		$array_sys["author"] = $_SESSION["user_name"] ? $_SESSION["user_name"] : "游客";
		$array_sys["author_type"] = $_SESSION["user_id"] ? "user" : "guest";//发布人类型
		$array_sys["ip"] = sys_ip();//发布人IP
		$array_sys["post_date"] = $this->system_time;
		if($id)
		{
			$array_sys["post_date"] = $rs["post_date"];
			$array_sys["modify_date"] = $this->system_time;
		}
		$array_sys["thumb_id"] = $this->trans_lib->int("thumb_id");
		if(!$id)
		{
			$array_sys["langid"] = $_SESSION["sys_lang_id"];
		}
		$array_sys["htmltype"] = "cateid";
		$array_sys["status"] = 0;
		//如果有分类
		if($cateid)
		{
			$condition = $_SESSION["user_id"] ? "c.ifuser='1'" : "c.ifguest='1'";
			$cate_rs = $this->cate_m->get_one($cateid,$condition);
			if(!$cate_rs)
			{
				error("分类不正确！",$this->url("post,set","id=".$id."&module_id=".$module_id));
			}
			$tmp_check_status = $_SESSION["user_id"] ? "chk_user" : "chk_guest";
			$array_sys["status"] = $cate_rs[$tmp_check_status];
		}
		else
		{
			$tmp_check_status = $_SESSION["user_id"] ? "u_free_check" : "g_free_check";
			$array_sys["status"] = $m_rs[$tmp_check_status];
		}
		$insert_id = $this->post_m->save_sys($array_sys,$id);//存储数据
		if(!$insert_id)
		{
			error("数据存储失败，请检查",site_url("post,set","id=".$id."&module_id=".$module_id));
		}
		unset($array_sys);//注销存储信息
		//存储电子商务信息
		if($m_rs["if_biz"])
		{
			$array_biz = array();
			$array_biz["id"] = $insert_id;
			$array_biz["price"] = $this->trans_lib->float("price");
			$array_biz["free"] = isset($_POST["free"]) ? 1 : 0;
			$array_biz["ifspecial"] = isset($_POST["ifspecial"]) ? 1 : 0;
			$array_biz["freeshipping"] = isset($_POST["freeshipping"]) ? 1 : 0;
			$array_biz["virtual"] = isset($_POST["virtual"]) ? 1 : 0;
			$array_biz["weight"] = $this->trans_lib->float("weight");
			$this->post_m->save_biz($array_biz);
			unset($array_biz);
		}
		//判断是否
		$extlist = $this->module_m->fields_index($module_id);
		foreach($extlist AS $key=>$value)
		{
			$array_ext = array();
			$array_ext["id"] = $insert_id;
			$array_ext["field"] = $value["identifier"];//扩展字段信息
			$format_type = $value["if_html"] ? "html" : "safe";
			$val = $this->trans_lib->$format_type($value["identifier"]);
			if(is_array($val))
			{
				$val = implode(",",$val);
			}
			$array_ext["val"] = $val;
			$this->post_m->save_ext($array_ext,$value["tbl"]);
		}
		//提示添加成功，进入跳转
		if($_SESSION["user_id"])
		{
			error("数据存储成功，请稍候……",site_url("post,list","module_id=".$module_id."&cate_id=".$cateid));
		}
		else
		{
			$r_url = $_SERVER["HTTP_REFERER"] ? $_SERVER["HTTP_REFERER"] : $this->url();
			error("您的信息已提交，请稍候……",$r_url);
		}
	}

	function del_f()
	{
		$id = $this->trans_lib->int("id");
		if(!$id)
		{
			exit("error: 删除失败，没有指定ID");
		}
		$rs = $this->post_m->get_one($id);
		if($rs["author"] != $_SESSION["user_name"] || $rs["author_type"] != "user")
		{
			exit("您不能删除非自己主题的信息。");
		}
		$this->post_m->del($id);
		exit("ok");
	}
}
?>